<?php
/**	
*	FILENAME: 		/config/restore_database.php
*	DESCRIPTION:	This script will remove all data in the current installation's database by removing the entire structure and creating it based on a MySQL dump of a fresh database.
*	AUTHOR:			Casey Burkhardt
*	VERSION:		1.0.0
*	LAST MODIFIED:	12/25/2009
**/

require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/database.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/config/environment.php");

$SQL_FILENAME = "mynova_mobile.sql";
$CONFIRMATION_STRING = "I understand this will erase the current database.";

if (($_POST["submit"]) && ($_POST["confirmation"] == $CONFIRMATION_STRING)) {
	if (file_exists($SQL_FILENAME)) {
		connectToDatabase();
		
		$databaseInfo = getDatabaseInformation();
		
		$sql = "SHOW TABLES FROM " . $databaseInfo["database"];
		if($result = mysql_query($sql)){
			while($row = mysql_fetch_row($result)) {
				$found_tables[]=$row[0];
			}
		} else {
			die("Error, could not list tables. MySQL Error: " . mysql_error());
		}
		if (!empty($found_tables)) {
			foreach($found_tables as $table_name){
				$sql = "DROP TABLE " . $databaseInfo["database"] . "." . $table_name . ";";
				if($result = mysql_query($sql)){
					echo "Deleted table " . $table_name . ".<br />";
				} else {
					echo "Error deleting table " . $table_name . ".  MySQL Error: " . mysql_error() . "<br />";
				}
			}
		} else {
			echo "Previous database is empty, writing new tables...<br />";	
		}
		
		echo "Executing MySQL Queries...<br />";
		$file = file_get_contents($SQL_FILENAME);
		$sql = explode(';', $file);
		$i = 0;
		foreach ($sql as $query) {
			if (!empty($query)) {
				$r = mysql_query($query);
				$i++;
			}
		}
		die($i . " Operations Completed Successfully.");
	}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MyNova Mobile Configuration: Erase Database</title>
</head>

<body>
<?php if(file_exists($SQL_FILENAME)) { ?>
<h1>Warning</h1>
<p>Clicking the &quot;Erase Database&quot; button below will completely erase the structure and data of this installation's associated database and replace it with the structure and data in the &quot;<?php echo $SQL_FILENAME; ?>&quot; file, shown below. You are urged to continue only if you understand the consequences of performing this action. To proceed, type &quot;<?php echo $CONFIRMATION_STRING; ?>&quot; in the field below and click &quot;Erase Database&quot;.</p>
<form id="form1" name="form1" method="post" action="">
  <p>
    <label>Confirmation
      <input name="confirmation" type="text" id="confirmation" size="50" />
    </label>
  </p>
  <p>
    <label>
      <input type="submit" name="submit" id="submit" value="Erase Database" />
    </label>
  </p>
</form>
<code>
<?php
	$file = file_get_contents($SQL_FILENAME);
	$file = str_replace("\n", "<br />", $file);
	echo $file;
?>
</code>
<?php } else { ?>
<h1>Error</h1>
<p>The file &quot;<?php echo $SQL_FILENAME; ?>&quot; was not found in the current directory. This file dictates the way in which the database should be reconstructed, and is required to continue.</p>
<?php } ?>
</body>
</html>